package lifeed;

import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;

import org.dom4j.Element;

public class ItemRSS {
    
    private String title = "";
    private String description = "";
    private String link = "";
    private Date pubDate = null;
    
    private Element itemElement = null;   // Documento DOM con la struttura del Podcast
    
    /** 
     * Crea una nuova istanza di Podcast impostando il documento da parsificare
    */
    public ItemRSS(Element document) {
        this.itemElement = document;
        parseItem();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Element getDocument() {
        return itemElement;
    }

    public void setDocument(Element document) {
        this.itemElement = document;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Date getPubDate() {
        return pubDate;
    }

    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        String buffer = "TITLE: " + title;
        buffer += "\nDESCRIPTION: " + description;
        buffer += "\nLINK: " + link;
        buffer += "\nPUBDATE: " + pubDate;
        return buffer;
    }

    // ### METODI PRIVATI ###
    private void parseItem() {
        this.title = itemElement.element("title") != null ? itemElement.element("title").getText().trim() : "";
        this.link = itemElement.element("link") != null ? itemElement.element("link").getText().trim() : "";
        this.description = itemElement.element("description") != null ? itemElement.element("description").getText().trim() : "";
        this.pubDate = itemElement.element("pubDate") != null ? parseDate(itemElement.element("pubDate").getText().trim()) : new Date();
    }


	public Date parseDate(String d) {
        String[] date_formats = { "yyyy-MM-dd'T'kk:mm:ss'Z'", // ISO
			"yyyy-MM-dd'T'kk:mm:ssz", // ISO
			"yyyy-MM-dd'T'kk:mm:ss", // ISO
			"EEE, dd MMM yy HH:mm:ss z", // RFC822
			"EEE, d MMM yyyy kk:mm:ss z", // RFC2882
			"EEE MMM  d kk:mm:ss zzz yyyy", // ASC
			"EEE, dd MMMM yyyy kk:mm:ss", // Disney Mon, 26 January 2004
											// 16:31:00 ET
			"yyyy-MM-dd kk:mm:ss.0", "-yy-MM", "-yyMM", "yy-MM-dd",
			"yyyy-MM-dd", "yyyy-MM", "yyyy-D", "-yyMM", "yyyyMMdd", "yyMMdd",
			"yyyy", "yyD"

        };
        Date date = null;
		SimpleDateFormat formatter;
		d = d.replaceAll("([-+]\\d\\d:\\d\\d)", "GMT$1"); // Correct W3C times
		d = d.replaceAll(" ([ACEMP])T$", " $1ST"); // Correct Disney times
		for (int i = 0; i < date_formats.length; i++) {
			try {
				formatter = new SimpleDateFormat(date_formats[i], Locale.ENGLISH);
				date = formatter.parse(d);
				break;
			} catch (Exception e) {}
		}

		return date;
	}
}